package com.example.alipay_10_3.impl;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.example.alipay_10_3.Vo.AlipayVo;
import com.example.alipay_10_3.config.AlipayConfig;
import com.example.alipay_10_3.server.PayService;
import org.springframework.stereotype.Service;

@Service
public class PayServiceImpl implements PayService {

    @Override
    public Object Alipay(AlipayVo alipayVo) throws Exception {
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(
                AlipayConfig.gatewayUrl,
                AlipayConfig.app_id,
                AlipayConfig.merchant_private_key,
                "json",
                AlipayConfig.charset,
                AlipayConfig.alipay_public_key,
                AlipayConfig.sign_type
        );
        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);

        AlipayTradePagePayModel model = new AlipayTradePagePayModel();
        model.setOutTradeNo(alipayVo.getOut_trade_no());
        model.setStoreId(String.valueOf(alipayVo.getId()));
        model.setTotalAmount(String.valueOf(alipayVo.getTotal_amount()));
        model.setSubject(alipayVo.getSubject());
        model.setTimeoutExpress("5m");
        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        alipayRequest.setBizModel(model);
        //请求
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        return result;
    }

}
